//
//  Value.swift
//  JWBaseLib
//
//  Created by 周季伟 on 2017/12/12.
//  Copyright © 2017年 周季伟. All rights reserved.
//

import Alamofire

class Cache {
    lazy var db = YYCache(name: "com.rebelbird.dahuang.Api")
    // url + method + parameters + headers 能够生成请求的唯一标识符
    func cahceKey(for urlString: String, method: HTTPMethod, parameters: [String: Any]?, headers: [String: String]?) -> String {
        return urlString + method.rawValue + String(describing: parameters) + String(describing: headers)
    }
    // 存储
    func save(value: [String: AnyObject], for key: String) {
        db?.setObject(value as NSCoding, forKey: key)
    }
    // 读取
    func load(for key: String) -> [String: AnyObject]? {
        return db?.object(forKey: key) as? [String: AnyObject]
    }
    // 清除
    func clear() {
        db?.removeAllObjects()
    }
}
